define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salemanagement/index',
                    add_url: 'salemanagement/add',
                    edit_url: 'salemanagement/edit',
                    del_url: 'salemanagement/del',
                    multi_url: 'salemanagement/multi',
                    import_url:'salemanagement/import',
                    table: 'salemanagement',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'admin_id', title: __('Admin_id')}, 
                        {field: 'acount', title: __('Acount')},
                        {field: 'instumentname', title: __('Instumentname')},
                        {field: 'instumenttype', title: __('Instumenttype')},
                        {field: 'instumentsno', title: __('Instumentsno')},
                        {field: 'saleman', title: __('Saleman'), formatter: function (value, row, index) { return (!value) ? __('None') : value; }},
                        {field: 'saletime', title: __('Saletime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.date},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'field1', title: __('Field1')},
                        // {field: 'field2', title: __('Field2')},
                        // {field: 'field3', title: __('Field3')},
                        // {field: 'field4', title: __('Field4')},
                        // {field: 'field5', title: __('Field5')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'salestatus', title: __('Salestatus'), visible:false, searchList: {"salestatus 0":__('Salestatus 0'),"salestatus 1":__('Salestatus 1')}},
                        {field: 'salestatus_text', title: __('Salestatus'), operate:false},
                        {field: 'servicestate', title: __('Servicestate'), visible:false, searchList: {"servicestate 1":__('Servicestate 1'),"servicestate 2":__('Servicestate 2'),"servicestate 3":__('Servicestate 3')}},
                        {field: 'servicestate_text', title: __('Servicestate'), operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                // 仪器名称
                $(document).on("change", "#c-instumentname", function () {

                    var iname = $(this).val();

                    // 清空仪器类型下拉数据
                    $('#c-instumenttype').empty(); 
                    
                    // 初始化仪器类型选择数据
                    var option = $("<option>").val('').text(__('Please Choice Instument Style')); 
                    $("#c-instumenttype").append(option); 

                    if(iname) {                    
                        $.get("/admin/ajax/instype?iname="+iname, function(data, status){
                            data =  $.parseJSON( data );
                            if(data.length) {                             
                                $.each(data, function(i, row){
                                    var option = $("<option>").val(row.name).text(row.name); 
                                    $("#c-instumenttype").append(option);  
                                });                                                       
                            }
                         });

                     //$(this).prop('disabled',true);
                    } 
                });
                
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});